﻿using System;
using System.Collections.Generic;
using SuperX.SuperSocket.Socket.Command;
using SuperX.SuperSocket.Socket.Message;
using SuperSocket.SocketBase;
using CloseReason = SuperSocket.SocketBase.CloseReason;

namespace SuperX.SuperSocket.Socket.Server
{
    /*
    AppServer 代表了监听客户端连接，承载TCP连接的服务器实例。理想情况下，我们可以通过AppServer实例获取任何你想要的客户端连接，服务器级别的操作和逻辑应该定义在此类之中。
    */

    public class SuperXSocketServer : AppServer<Session, MessageRequestInfo>
    {
        public SuperXSocketServer() : base(new MessageReceiveFilterFactory())//注册自己的过滤工厂
        { }
    }

    /* AppSession 代表一个和客户端的逻辑连接，基于连接的操作应该定于在该类之中。你可以用该类的实例发送数据到客户端，接收客户端发送的数据或者关闭连接。
     * AppSession是Socket中的一个连接会话。
    */

    public class Session : AppSession<Session, MessageRequestInfo>
    {
        /// <summary>
        /// 连接事件信息
        /// </summary>
        protected override void OnSessionStarted()
        {
            Send($"{XSocketMessage.ConnectedConst}\r\n");
        }

        /// <summary>
        /// 不可识别事件信息
        /// </summary>
        /// <param name="requestInfo"></param>
        protected override void HandleUnknownRequest(MessageRequestInfo requestInfo)
        {
            Send($"{XSocketMessage.UnknowConst}\r\n");
        }

        /// <summary>
        /// 错误事件信息
        /// </summary>
        /// <param name="e"></param>
        protected override void HandleException(Exception e)
        {
            Send($"{XSocketMessage.ErrorConst}: {0}\r\n", e.Message);
        }

        /// <summary>
        /// 关闭Session事件信息
        /// </summary>
        /// <param name="reason"></param>
        protected override void OnSessionClosed(CloseReason reason)
        {
            Send($"{XSocketMessage.ConnectedConst} \r\n");
        }

        /// <summary>
        /// 发送Socket信息
        /// </summary>
        /// <param name="message"></param>
        public void SendMessage(SocketMessage message)
        {
            var data = message.GetSendBytes();
            Send(data, 0, data.Length);
        }

        /// <summary>
        /// 发送字符串类型的信息，内容在body里
        /// </summary>
        /// <param name="content"></param>
        public override void Send(string content)
        {
            var appMsg = new XSocketMessage() { Content = content };
            SendMessage(new SocketMessage() { Body = appMsg.Datas });
        }

        /// <summary>
        /// 发送带参数的字符信息
        /// </summary>
        /// <param name="message"></param>
        /// <param name="paramValues"></param>
        public override void Send(string message, params object[] paramValues)
        {
            var content = string.Format(message, paramValues);
            Send(content);
        }

        /// <summary>
        /// 发送头部信息与内容信息
        /// </summary>
        /// <param name="header"></param>
        /// <param name="content"></param>
        public void SendWithHeader(string header, string content)
        {
            var appMsg = new XSocketMessage() { Content = content };
            SendMessage(new SocketMessage() { Header = header, Body = appMsg.Datas });
        }

        /// <summary>
        /// 发送头部信息与内容信息
        /// </summary>
        /// <param name="headers"></param>
        /// <param name="content"></param>
        public void SendWithHeaders(Dictionary<string, string> headers, string content)
        {
            var appMsg = new XSocketMessage() { Content = content };
            SendMessage(new SocketMessage() { Headers = headers, Header = CommandTransfer.Serialize(headers), Command = "SendObjectWithHeaders", Body = appMsg.Datas });
        }
    }
}